<?php

class TemplatesController extends Controller {
  
  public function init()
  {
      if (Yii::app()->user->isGuest){
          $this->redirect('/users/signin');
      }
    if (Yii::app()->request->isAjaxRequest) {
      $this->layout = false;
    }
    parent::init();
  }


  public function actionIndex()
  {
    $this->redirect('/templates/list');
  }

  public function actionList()
  {
      $templates = Templates::model()->findAll();
      $companies = Companies::model()->getCompanies();
      $this->render('list', array('templates' => $templates, 'companies' => $companies));
  }

  public function actionAdd()
  {
    $data = Yii::app()->request->getPost('data', false);
    $products = Yii::app()->request->getPost('products');
    //$products = Yii::app()->request->getPost('products', false);
    $companies = Companies::model()->getCompanies();
    $discounts = Yii::app()->request->getPost('discounts');
    $minqty = Yii::app()->request->getPost('minqty');
    //$discounts = Yii::app()->request->getPost('discounts', false);
    if ($data){
      
      $settings = array();
      $minquantity = array();
      if((isset($discounts)) && (count($discounts) > 0)){
            
          foreach($discounts as $key => $value){
            $params = explode('-', $key);
            $settings[$params[1]][] = $params[2];
          }
          
          if((isset($minqty))){
                foreach($minqty as $key => $value){
                $params = explode('-', $key);
                $minquantity[$params[1]][] = $params[2];
              }
          }
    
          $template = new Templates();
          $template->title = $data['name'];
          $template->settings = CJSON::encode($settings);
          $template->min_quantity = CJSON::encode($minquantity);
          $template->company_id = $data['company_id'];
          $template->save();
          
          if((isset($products)) && (count($products) > 0)){
                foreach($products as $product){
                $template_product = new TemplatesProducts();
                $template_product->template_id = $template->id;
                $template_product->product_id = $product;
                $template_product->save();
              }
          }else{
            $this->redirect('/templates/list');
          }
          $this->render('success', array('template' => $template));
        }else{
            $this->render('add', array('companies' => $companies));
        }
    }else{
      $this->render('add', array('companies' => $companies));
    }
  }

  public function actionAjax()
  {
    $company_id = Yii::app()->request->getPost('company', false);
    $discounts = array('20' => true, '40' => true, '75' => true, '100' => true);
	$minqty_default = array('1' => true, '3' => true, '6' => true);
	
    $categories = ProductsCategories::model()->findAllByAttributes(array('company_id' => $company_id));
    $result = array();
    foreach ($categories as $category){
      $temp = array();
      $temp['category'] = $category;
      $temp['products'] = Products::model()->findAllByAttributes(array('category_id' => $category->id));
      $result[] = $temp;
    }
    $this->render('ajax', array('search' => $result, 'discounts' => $discounts,'minqty_default'=>$minqty_default));
  }

  public function actionEdit($id)
  {
    if($id){
        $templates = Templates::model()->findByPk($id);
        if(isset($templates)){
            $settings_exist = json_decode($templates->settings);
            $companies_exist = Companies::model()->findByPk($templates->company_id);
            $categories = ProductsCategories::model()->findAllByAttributes(array('company_id' => $templates->company_id));
            $discounts_exist = array('20' => true, '40' => true, '75' => true, '100' => true);
			$minqty_default = array('1' => true, '3' => true, '6' => true);
			$minqty_exist = json_decode($templates->min_quantity);
            $result = array();
            foreach ($categories as $category){
              $temp = array();
              $temp['category'] = $category;
              $temp['products'] = Products::model()->findAllByAttributes(array('category_id' => $category->id));
              $result[] = $temp;
            }
			
			$dataProducts = TemplatesProducts::model()->findAllByAttributes(array('template_id' => $id));
            
            $action = Yii::app()->request->getPost('action', false);
            if($action == 'edit_template'){
				$products = Yii::app()->request->getPost('products');
				$companies = Companies::model()->getCompanies();
				$discounts = Yii::app()->request->getPost('discounts');
				$minqty = Yii::app()->request->getPost('minqty');
                  $settings = array();
                  $minquantity = array();
                  if((isset($discounts)) && (count($discounts) > 0)){
                      foreach($discounts as $key => $value){
                        $params = explode('-', $key);
                        $settings[$params[1]][] = $params[2];
                      }
                      if((isset($minqty))){
                            foreach($minqty as $key => $value){
                            $params = explode('-', $key);
                            $minquantity[$params[1]][] = $params[2];
                          }
                      }
                      
                      
                      $templates->min_quantity = CJSON::encode($minquantity);
                      $templates->settings = CJSON::encode($settings);
                      $templates->update();
                      $array_compair = array();
                      if((isset($products)) && (count($products) > 0)){
                          foreach($products as $product){
                            $template_product = TemplatesProducts::model()->findByAttributes(array('template_id'=>$templates->id, 'product_id'=>$product));
                            if(isset($template_product)){
                               
                            }else{
                                $template_product_new = new TemplatesProducts();
                                $template_product_new->template_id = $templates->id;
                                $template_product_new->product_id = $product;
                                $template_product_new->save();
                            }
                          }
                      }
                      $this->redirect('/templates/list');
                  }else{
                    $this->render('edit', array(
                    'templates' => $templates, 
                    'search'=>$result,
					'dataProducts'=>$dataProducts,
                    'companies_exist'=>$companies_exist,
                    'discounts_exist'=>$discounts_exist,
                    'settings_exist'=>$settings_exist,
					'minqty_default'=>$minqty_default,
					'minqty_exist'=>$minqty_exist
                  ));
                  }
            }else{
                $this->render('edit', array(
                'templates' => $templates, 
                'search'=>$result,
				'dataProducts'=>$dataProducts,
                'companies_exist'=>$companies_exist,
                'discounts_exist'=>$discounts_exist,
                'settings_exist'=>$settings_exist,
				'minqty_default'=>$minqty_default,
					'minqty_exist'=>$minqty_exist
              ));
            } 
        }else{
            $this->redirect('/templates/list');
        }
        }else{
                $this->render('edit', array(
                'templates' => $templates, 
                'search'=>$result,
				'dataProducts'=>$dataProducts,
                'companies_exist'=>$companies_exist,
                'discounts_exist'=>$discounts_exist,
                'settings_exist'=>$settings_exist,
				'minqty_default'=>$minqty_default,
				'minqty_exist'=>$minqty_exist
              ));
        }
  }

  public function actionDelete($id)
  {
    if ($id){
        $templates = Templates::model()->findByPk($id);
        $templates_product = TemplatesProducts::model()->findAllByAttributes(array('template_id'=>$templates->id));
        foreach($templates_product as $item){
            $tmp = TemplatesProducts::model()->findByPk($item->id);
            $tmp->delete();
        }
        $templates->delete();
        $this->redirect('/templates/list');
    }else{
      $this->redirect('/templates/list');
    }
  }

}